#include <bits/stdc++.h>

using namespace std;

class Solution
{
public:
    vector<int> sortedSquares(vector<int> &nums)
    {
        // for (auto &num : nums)
        // {
        //     num = num * num;
        // }
        // sort(nums.begin(), nums.end());
        // return nums;
        int length = nums.size();
        vector<int> res;
        res.resize(length);
        int i = 0;
        int j = length - 1;
        int k = length - 1;
        while (i <= j)
        {
            if (nums[i] * nums[i] <= nums[j] * nums[j])
            {
                res[k] = nums[j] * nums[j];
                k--;
                j--;
            }
            else
            {
                res[k] = nums[i] * nums[i];
                k--;
                i++;
            }
        }
        return res;
    }
};

Solution sol;

int main()
{
    vector<int> vec = {-4, -1, 0, 3, 10};
    vector<int> res = sol.sortedSquares(vec);
    for (auto num : res)
    {
        cout << num << " ";
    }
    cout << endl;
    return 0;
}